package io.shuttle.mbe.api

import io.shuttle.mbe.api.annotation.ChromeMinVersion
import io.shuttle.mbe.api.annotation.PromiseStyleMinVersion
import io.shuttle.mbe.api.types.Value1Function
import io.shuttle.mbe.api.types.VoidFunction
import io.shuttle.mbe.core.Promise

////////////////////
// Browsing Data
////////////////////
/**
 * Use the `Browser.browsingData` API to remove browsing data from a user's local profile.
 *
 * Permissions: "browsingData"
 */
interface BrowsingData {
    // Clears various types of browsing data stored in a user's profile.
    @PromiseStyleMinVersion(96)
     fun remove(
        options: RemovalOptions,
        dataToRemove: DataTypeSet,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Clears websites' appcache data.
    @PromiseStyleMinVersion(96)
     fun removeAppcache(
        options: RemovalOptions,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Clears the browser's cache.
    @PromiseStyleMinVersion(96)
     fun removeCache(
        options: RemovalOptions,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Clears websites' cache storage data.
    @PromiseStyleMinVersion(96)
     fun removeCacheStorage(
        options: RemovalOptions,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Clears the browser's cookies and server-bound certificates modified within a particular timeframe.
    @PromiseStyleMinVersion(96)
     fun removeCookies(
        options: RemovalOptions,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Clears the browser's list of downloaded files (not the downloaded files themselves).
    @PromiseStyleMinVersion(96)
     fun removeDownloads(
        options: RemovalOptions,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Clears websites' file system data.
    @PromiseStyleMinVersion(96)
     fun removeFileSystems(
        options: RemovalOptions,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Clears the browser's stored form data (autofill).
    @PromiseStyleMinVersion(96)
     fun removeFormData(
        options: RemovalOptions,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Clears the browser's history.
    @PromiseStyleMinVersion(96)
     fun removeHistory(
        options: RemovalOptions,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Clears websites' IndexedDB data.
    @PromiseStyleMinVersion(96)
     fun removeIndexedDB(
        options: RemovalOptions,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Clears websites' local storage data.
    @PromiseStyleMinVersion(96)
     fun removeLocalStorage(
        options: RemovalOptions,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Clears the browser's stored passwords.
    @PromiseStyleMinVersion(96)
     fun removePasswords(
        options: RemovalOptions,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Clears plugins' data.
    @PromiseStyleMinVersion(96)
    @Deprecated(
        "Deprecated since Chrome 88",
        ReplaceWith("Support for Flash has been removed. This function has no effect.")
    )
     fun removePluginData(
        options: RemovalOptions,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Clears websites' service workers.
    @ChromeMinVersion(72)
    @PromiseStyleMinVersion(96)
     fun removeServiceWorkers(
        options: RemovalOptions,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Clears websites' WebSQL data.
    @PromiseStyleMinVersion(96)
     fun removeWebSQL(
        options: RemovalOptions,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Reports which types of data are currently selected in the 'Clear browsing data' settings UI.
    // Note: some of the data types included in this API are not available in the settings UI,
    // and some UI settings control more than one data type listed here.
    @PromiseStyleMinVersion(96)
     fun settings(
        callback: Value1Function<BrowsingDataSetting>? = null
    ): Promise<BrowsingDataSetting>

    // A set of data types. Missing data types are interpreted as false.
    data class DataTypeSet(
        // Websites' appcaches.
        var appcache: Boolean? = null,
        // The browser's cache.
        var cache: Boolean? = null,
        // Cache storage
        @ChromeMinVersion(72)
        var cacheStorage: Boolean? = null,
        // The browser's cookies.
        var cookies: Boolean? = false,
        // The browser's download list.
        var downloads: Boolean? = null,
        // Websites' file systems.
        var fileSystems: Boolean? = null,
        // The browser's stored form data.
        var formData: Boolean? = null,
        // The browser's history.
        var history: Boolean? = null,
        // Websites' IndexedDB data.
        var indexedDB: Boolean? = null,
        // Websites' local storage data.
        var localStorage: Boolean? = null,
        // Stored passwords.
        var passwords: Boolean? = null,
        @Deprecated(
            "Deprecated since Chrome 88",
            ReplaceWith("Support for Flash has been removed. This data type will be ignored.")
        )
        var pluginData: Boolean? = null,
        @Deprecated(
            "Deprecated since Chrome 76",
            ReplaceWith("Support for server-bound certificates has been removed. This data type will be ignored.")
        )
        var serverBoundCertificates: Boolean? = null,
        var serviceWorkers: Boolean? = null,
        // Websites' WebSQL data.
        var webSQL: Boolean? = null,
    )

    // Options that determine exactly what data will be removed.
    data class RemovalOptions(
        // When present, data for origins in this Array is excluded from deletion.
        // Can't be used together with origins.
        // Only supported for cookies, storage and cache.
        // Cookies are excluded for the whole registrable domain.
        @ChromeMinVersion(74)
        var excludeOrigins: List<String>? = null,
        /** An object whose properties specify which origin types ought to be cleared. If this object isn't specified, it defaults to clearing only "unprotected" origins. Please ensure that you _really_ want to remove application data before adding 'protectedWeb' or 'extensions'. */
        var originTypes: OriginTypes? = null,
        // When present, only data for origins in this list is deleted.
        // Only supported for cookies, storage and cache.
        // Cookies are cleared for the whole registrable domain.
        @ChromeMinVersion(74)
        var origins: List<String>? = null,
        // Remove data accumulated on or after this date,
        // represented in milliseconds since the epoch (accessible via the getTime method of the JavaScript Date object).
        // If absent, defaults to 0 (which would remove all browsing data).
        var since: Number? = null,
    )

    // An object whose properties specify which origin types ought to be cleared.
    // If this object isn't specified, it defaults to clearing only "unprotected" origins.
    // Please ensure that you really want to remove application data before adding 'protectedWeb'
    // or 'extensions'.
    data class OriginTypes(
        // Extensions and packaged applications a user has installed (be _really_ careful!).
        var extension: Boolean? = null,
        // Websites that have been installed as hosted applications (be careful!).
        var protectedWeb: Boolean? = null,
        // Normal websites.
        var unprotectedWeb: Boolean? = null,
    )

    data class BrowsingDataSetting(
        // All of the types will be present in the result,
        // with values of true if they are permitted to be removed (e.g., by enterprise policy)
        // and false if not.
        val dataRemovalPermitted: DataTypeSet,
        // All of the types will be present in the result,
        // with values of true if they are both selected to be removed and permitted to be removed,
        // otherwise false.
        val dataToRemove: DataTypeSet,
        val options: RemovalOptions,
    )

    data class SettingsResult(
        val options: RemovalOptions,
        /** All of the types will be present in the result, with values of `true` if they are both selected to be removed and permitted to be removed, otherwise `false`. */
        val dataToRemove: DataTypeSet,
        /** All of the types will be present in the result, with values of `true` if they are permitted to be removed (e.g., by enterprise policy) and `false` if not. */
        val dataRemovalPermitted: DataTypeSet
    )
}